﻿using UnityEngine;
using System.Collections;
using TTGame;
using TTGameEngine;

namespace CinemaDirector {
[CutsceneItemAttribute("LT Trigger", "Skill Play Animation", CutsceneItemGenre.ActorItem)]
public class SkillPlayAnimEvent : CinemaActorAction {
    public string animName;
    public float animTime;
    public bool animSyn = true;
    public bool isLoop = false;
    //朝向同步选项，同步于移动方向或者同步于面向方向（即摇杆）
    public LookSkillSyn lookSyn = LookSkillSyn.None;

    SkillAnimInfo animInfo = new SkillAnimInfo();

    Puppet m_owner;

    public override void Trigger(GameObject Actor) {
        SkillTL skill = this.GetSkill();
        //fixed:停止技能脚本时，脚本上所有剩余的触发器都会触发一次。
        if (skill.IsStopped()) { return; }

        animInfo.Init(animName, animTime, animSyn, lookSyn, isLoop);
        if(animInfo.isLoop) {
            skill.RegistLoopAnimEvent(this);
        }
        m_owner = Actor.GetComponent<Puppet>();
        m_owner.playSkillAnim(animInfo);
        EventLog("Trigger");
    }

    public override void End(GameObject Actor) {
        if(animInfo.isLoop) {
            EventLog("Skill Looping...");
            //不会重新触发当前Trigger。想要重新触发当前Trigger可以用(this.Firetime-0.001f)
            this.Cutscene.SetRunningTimeSafe(this.Firetime);
        }
    }

    public void BreakLoop(bool skipCurAnim) {
        if(animInfo.isLoop) {
            EventLog("Skill BreakLoop");
            animInfo.BreakLoop(skipCurAnim);
            if(skipCurAnim) {
                this.Cutscene.SetRunningTimeSafe(this.Firetime);
                //             this.Cutscene.Pause();
                //             this.Cutscene.SetRunningTime(this.Firetime + this.Duration);
                //             this.Cutscene.Resume();
            }
        }
    }

    void EventLog(string msgformat, params object[] args) {
        if(SkillTimelineSystem.SkillSystemLogFlag == false) {
            return;
        }
        DebugUtil.LogFormat("[SkillAnimEvent]obj:[{0}][{1}/{2}] {3}",
                            m_owner != null ? m_owner.GetGID() : "No Caster",
                            this.Cutscene.RunningTime.ToString(),
                            this.Cutscene.Duration.ToString(),
                            string.Format(msgformat, args));
    }
}
}